﻿using System;
using Glue;
using Xunit;

namespace GlueTests.MapperTests
{
    public class When_using_interfaces
    {
        public class GuiPerson {public IGuiAddress Address { get; set; }}
        public interface IGuiAddress { String ZipCode { get; set; } }
        public class GuiAddress:IGuiAddress { public String ZipCode { get; set; } }

        public class FlattenedDomainPerson { public String ZipCode { get; set; } }

        public class NestedDomainPerson { public DomainAddress Address { get; set; } }
        public class DomainAddress { public String ZipCode { get; set; } }

        [Fact]
        public void Should_map_normally_when_flattening()
        {
            var mapping = new Mapping<GuiPerson, FlattenedDomainPerson>();
            var addressMapping = new Mapping<IGuiAddress, FlattenedDomainPerson>();
            addressMapping.Relate(gui => gui.ZipCode, domain => domain.ZipCode);
            mapping.Flatten(domain => domain.Address, addressMapping);

            var guiPerson = new GuiPerson {Address = new GuiAddress {ZipCode = "1234"}};

            var domainPerson = mapping.Map(guiPerson, new FlattenedDomainPerson());
        }

        [Fact]
        public void Should_map_normally_with_nested_classes()
        {
            var mapping = new Mapping<GuiPerson, NestedDomainPerson>();
            var addressMapping = new Mapping<IGuiAddress, DomainAddress>();
            addressMapping.Relate(gui => gui.ZipCode, domain => domain.ZipCode);
            mapping.Relate(domain => domain.Address, gui=>gui.Address, addressMapping);

            var guiPerson = new GuiPerson { Address = new GuiAddress { ZipCode = "1234" } };

            var domainPerson = mapping.Map(guiPerson, new NestedDomainPerson{Address = new DomainAddress()});
        }

        [Fact]
        public void Should_map_normally_with_nested_classes_reverse()
        {
            var mapping = new Mapping<NestedDomainPerson,GuiPerson>();
            var addressMapping = new Mapping<DomainAddress,IGuiAddress>();
            addressMapping.Relate(gui => gui.ZipCode, domain => domain.ZipCode);
            mapping.Relate(domain => domain.Address, gui => gui.Address, addressMapping);

            var domainPerson = new NestedDomainPerson { Address = new DomainAddress { ZipCode = "1234" } };

            var guiPerson = mapping.Map(domainPerson, new GuiPerson() { Address = new GuiAddress() });
        }
    }
}
